package com.itheima.dao.daoImpl;

import com.itheima.dao.UserDao;
import com.itheima.domain.User;
import com.itheima.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao {

    @Override
    public List<User> findAll() {
        Connection conn = null;
        Statement stat = null;
        ResultSet rs = null;
        try {
            //1. 获取驱动
            //2. 获取连接对象
            conn = JDBCUtils.getConnection();
            //3. 获取语句执行体对象
            stat = conn.createStatement();

            //4. 编写sql
            String sql = "select * from user";

            //5. 执行sql
            rs = stat.executeQuery(sql);

            //5.1 创建list集合
            List<User> list = new ArrayList<>();

            //6. 处理结果集
            while (rs.next()) {
                int id = rs.getInt("id");
                String name = rs.getString("name");
                String gender = rs.getString("gender");
                int age = rs.getInt("age");
                String address = rs.getString("address");
                String qq = rs.getString("qq");
                String email = rs.getString("email");

                //添加对象到list集合
                User user = new User(id, name, gender, age, address, qq, email);
                list.add(user);
            }
            //8. 返回查询到的用户集合
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //添加学生
    @Override
    public void addUser(User user) {
        Connection conn = null;
        PreparedStatement stat = null;
        try {
            //1. 获取驱动
            //2. 获取连接对象
            conn = JDBCUtils.getConnection();

            //3. 编写sql
            String sql = "INSERT INTO USER VALUES (NULL,?,?,?,?,?,?)";

            //4. 获取语句执行体对象
            stat = conn.prepareStatement(sql);

            //5. 给占位符赋值
            stat.setString(1, user.getName());
            stat.setString(2, user.getGender());
            stat.setInt(3, user.getAge());
            stat.setString(4, user.getAddress());
            stat.setString(5, user.getQq());
            stat.setString(6, user.getEmail());

            //5. 执行sql
            stat.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //删除学生
    @Override
    public void deleteById(String id) {
        Connection conn = null;
        Statement stat = null;
        try {
            //1. 获取驱动
            //2. 获取连接对象
            conn = JDBCUtils.getConnection();
            //3. 获取语句执行体对象
            stat = conn.createStatement();

            //4. 编写sql
            String sql = "DELETE FROM USER WHERE id=" + id;

            //5. 执行sql
            stat.executeUpdate(sql);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //通过id查找
    @Override
    public User findById(String uid) {
        Connection conn = null;
        Statement stat = null;
        ResultSet rs = null;
        try {
            //1. 获取驱动
            //2. 获取连接对象
            conn = JDBCUtils.getConnection();
            //3. 获取语句执行体对象
            stat = conn.createStatement();
            //4. 编写sql
            String sql = "select * from user WHERE  id=" + uid;

            //5. 执行sql
            rs = stat.executeQuery(sql);

            //6. 处理结果集
            if (rs.next()) {
                int id = rs.getInt("id");
                String name = rs.getString("name");
                String gender = rs.getString("gender");
                int age = rs.getInt("age");
                String address = rs.getString("address");
                String qq = rs.getString("qq");
                String email = rs.getString("email");

                //添加对象到list集合
                User user = new User(id, name, gender, age, address, qq, email);
                //8. 返回查询到的用户集合
                return user;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //更改信息
    @Override
    public void updateUser(User user) {
        Connection conn = null;
        PreparedStatement stat = null;
        try {
            //1. 获取驱动
            //2. 获取连接对象
            conn = JDBCUtils.getConnection();


            //4. 编写sql
            String sql = "UPDATE USER  set name = ?,gender = ?,age = ?, address = ?, qq = ?, email = ? where id = ?";
            //3. 获取语句执行体对象
            stat = conn.prepareStatement(sql);

            //为占位符赋值
            stat.setString(1, user.getName());
            stat.setString(2, user.getGender());
            stat.setInt(3, user.getAge());
            stat.setString(4, user.getAddress());
            stat.setString(5, user.getQq());
            stat.setString(6, user.getEmail());
            stat.setInt(7, user.getId());


            //5. 执行sql
            stat.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
